$(function() {
    var settings = {
        url: ctx + "dict/list",
        pageSize: 10,
        queryParams: function(params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                keyy: null
            };
        },
        columns: [{
                checkbox: true
            },
            {
                field: 'dictId',
                title: '字典ID',
                width: 150
            }, {
                field: 'keyy',
                title: '键'
            }, {
                field: 'valuee',
                title: '值'
            }, {
                field: 'tableName',
                title: '表名'
            }, {
                field: 'fieldName',
                title: '字段名'
            }
        ]
    };
    $JuXue.initTable('dictTable', settings);
});

function refresh() {
    $JuXue.refreshTable('dictTable');
}
/*删除字典*/
function deleteDicts() {
    var selected = $("#dictTable").bootstrapTable('getSelections');
    var selected_length = selected.length;
    if (!selected_length) {
        $JuXue.n_danger('请勾选需要删除的字典！');
        return;
    }
    var ids = "";
    for (var i = 0; i < selected_length; i++) {
        ids += selected[i].dictId;
        if (i !== (selected_length - 1)) ids += ",";
    }
    $JuXue.confirm({
        text: "确定删除选中的字典？",
        confirmButtonText: "确定删除"
    }, function() {
        $.post(ctx + 'dict/delete', { "ids": ids }, function(r) {
            if (r.code === 0) {
                $JuXue.n_success(r.msg);
                refresh();
            } else {
                $JuXue.n_danger(r.msg);
            }
        });
    });
}

function exportDictExcel(){
	$.post(ctx+"dict/excel",$(".dict-table-form").serialize(),function(r){
		if (r.code === 0) {
			window.location.href = "file/download?fileName=" + r.msg + "&delete=" + true;
		} else {
            $JuXue.n_warning(r.msg);
		}
	});
}

/*加载新增页面*/
function addDictionary(){
    var url = ctx + "dict/loadAddDict";
    $.post(url,{},function(result){
        $("#dict-info").empty().append(result).modal('show').draggable();
    })
}

/*加载修改页面*/
function loadUpdate(){
    const selected = $("#dictTable").bootstrapTable('getSelections');
    const selected_length = selected.length;
    if (!selected_length) {$JuXue.n_warning('请勾选需要修改的字典！');return;}
    if (selected_length > 1) {$JuXue.n_warning('一次只能修改一条字典！');return;}
    $.post(ctx+"dict/loadUpdateDict",{},function (r) {$("#dict-info").empty().append(r).modal("show").draggable();});
}